
package com.kaciula.letsdoit.ui;

import com.kaciula.letsdoit.R;
import com.kaciula.letsdoit.db.WastesDbAdapter;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class WastesCursorAdapter extends SimpleCursorAdapter {

    private int mLayout;

    public WastesCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.mLayout = layout;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        View row = inflater.inflate(mLayout, parent, false);

        ViewWrapper wrapper = new ViewWrapper(row);
        row.setTag(wrapper);

        return row;
    }

    @Override
    public void bindView(View row, Context ctx, Cursor c) {
        int noCol = c.getColumnIndex(WastesDbAdapter.KEY_ROWID);
        String name = c.getString(noCol);

        ViewWrapper wrapper = (ViewWrapper) row.getTag();
        TextView tv = wrapper.getName();
        tv.setText(ctx.getString(R.string.waste) + " " + name);
    }

    private class ViewWrapper {
        
        private View mBase;

        private TextView mName = null;

        private ViewWrapper(View base) {
            this.mBase = base;
        }

        private TextView getName() {
            if (mName == null) {
                mName = (TextView) mBase.findViewById(android.R.id.text1);
            }

            return mName;
        }
    }
}
